package org.sgx.htmlminimizator.client;

import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor2;
import org.sgx.htmlminimizator.client.res.Resources;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;

public class MainPanel extends FlexTable {
	private Config config;
	private PropertyHaverEditor2<Config> configEd;
	private TextArea htmlIn;
	private TextArea htmlOut;

	public MainPanel() {
		
		MenuBar menuBar = new MenuBar(false);
		setWidget(0, 0, menuBar);
		menuBar.setWidth("100%");
		MenuBar menuBar_1 = new MenuBar(true);
		
		MenuItem mntmHelp = new MenuItem("Help", false, menuBar_1);
		
		MenuItem mntmNewItem = new MenuItem("About...", false, new Command() {
			
			@Override
			public void execute() {
				GUIUtil.acceptHtmlDialog("About sgurin html minimizator", 
					Resources.INSTANCE.about_html()); 
			}
		});
		
		menuBar_1.addItem(mntmNewItem);
		menuBar.addItem(mntmHelp);
		
		FlexTable flexTable = new FlexTable();
		setWidget(2, 0, flexTable);
		getCellFormatter().setHeight(2, 0, "100%");
		flexTable.setSize("100%", "100%");
		
		Label lblNewLabel = new Label("Inser your HTML code in the following text area and press Accept");
		flexTable.setWidget(0, 0, lblNewLabel);
		flexTable.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
		
		VerticalPanel verticalPanel_1 = new VerticalPanel();
		flexTable.setWidget(2, 0, verticalPanel_1);
		verticalPanel_1.setSize("100%", "100%");
		
		htmlIn = new TextArea();
		verticalPanel_1.add(htmlIn);
		verticalPanel_1.setCellHeight(htmlIn, "100%");
		verticalPanel_1.setCellWidth(htmlIn, "100%");
		htmlIn.setSize("100%", "100%");
		
		VerticalPanel verticalPanel = new VerticalPanel();
		flexTable.setWidget(4, 0, verticalPanel);
		verticalPanel.setSize("100%", "100%");
		
		htmlOut = new TextArea();
		verticalPanel.add(htmlOut);
		verticalPanel.setCellHeight(htmlOut, "100%");
		verticalPanel.setCellWidth(htmlOut, "100%");
		htmlOut.setSize("100%", "100%");
		getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		
		//configPanel
		VerticalPanel configPanel = new VerticalPanel();
		configPanel.getElement().addClassName("config-panel"); 
		setWidget(2, 1, configPanel);
		getCellFormatter().setWidth(2, 1, "40%");
		configPanel.setWidth("");
		
		new EditorFramework1().start(); //do not forget of initialize the gwteditors framework !
		
		config = GWT.create(Config.class); 
		config.loadDefault(); 
		
		configEd = new PropertyHaverEditor2<Config>(); 
		configEd.load(config);
		
		HTML lblNewLabel_1 = new HTML(Resources.INSTANCE.config_props_label1().getText());
		configPanel.add(lblNewLabel_1);
		configPanel.add(configEd); 
		
		SimplePanel simplePanel = new SimplePanel();
		setWidget(3, 0, simplePanel);
		simplePanel.setHeight("16px");
		
		FlowPanel flowPanel = new FlowPanel();
		setWidget(4, 0, flowPanel);
		
		Button btnNewButton = new Button("Accept", new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				
				Config config2 = configEd.flush();
				StringBuffer out = new StringBuffer(); 
				try {
					new HTMLMinimizatorUtil().minimize(config2, htmlIn.getText(), out);
				}catch (Exception e) {
					Window.alert("exception parsing XML : "+e+"\n - Cause: "+e.getCause()); 
				}
				htmlOut.setText(out.toString());
				
			}
		}); 
		flowPanel.add(btnNewButton);
		getCellFormatter().setVerticalAlignment(2, 1, HasVerticalAlignment.ALIGN_TOP);
		getCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_RIGHT);
	}

}
